/**
 * 
 */

$(function() {
	$(".error").hide();
	$(".new-button").click(function() {
		$('.error').hide();  
		var date = encodeURIComponent($("input#date").val());
		var price = encodeURIComponent($("input#price").val());  
		if (price == "") {  
			$("label#price_error").show();  
			$("input#price").focus();  
			return false;  
		}
		var instrumentId = encodeURIComponent($("input#instrumentId").val());  
		var dataString = 'date=' + date + '&price=' + price + '&instrumentId=' + instrumentId;
		$.ajax({  
			type: "POST",  
			url: "StorePrice",  
			data: dataString,  
			success: function(msg) {  
				if(msg.indexOf("Error") == -1) {
					var jsonExpression = "(" + msg + ")";
					var price = eval(jsonExpression);
					addPrice(price);
				} else {
					alert(msg);
				}
			}
		});
		return false;
	});  
	
	function addPrice(price) {
		var rowPos = findPositionForNewPrice(price);
		
		var tbl = document.getElementById('tblPrices');
		var priceCount = tbl.rows.length-1;
		var row = tbl.insertRow(rowPos);
		row.setAttribute("id","row" + price.priceId);
		
		var cell = row.insertCell(0);
		var date = new Date(price.date);
		cell.className="numeric";
		var textNode = document.createTextNode(formatDateTime(date));
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		cell.className="numeric";
		textNode = document.createTextNode(Number(price.price).toFixed(4));
		cell.appendChild(textNode);
		
		cell = row.insertCell(row.cells.length);
		var element = document.createElement("Button");
		element.appendChild(document.createTextNode("Delete"))
		element.className="button delete-button";
		element.setAttribute("onclick","deletePrice(" + price.priceId + ")");
		element.setAttribute("id","btn" + price.priceId);
		cell.appendChild(element);
		
		cell = row.insertCell(row.cells.length);
		cell.style.display="none";
		textNode = document.createTextNode(price.date);
		cell.appendChild(textNode);
	}
	
	function findPositionForNewPrice(price) {
		var tbl = document.getElementById('tblPrices');
		var count = tbl.rows.length;
		var i=1;
		if (count != 1) {
			while(i < count) {
				var date = new Date(price.date);
				if(date > Number(tbl.rows[i].cells[3].firstChild.nodeValue)) {
					return i;
				}
				i++;
			}
		}
		return count;
	}
});

function deletePrice(priceId) {
	var dataString = 'priceId='+ encodeURIComponent(priceId);
	$.ajax({
		type: "POST",  
		url: "DeletePrice",  
		data: dataString,  
		success: function(msg) {  
			if(msg.indexOf("Error") == -1) {
				document.getElementById('tblPrices').deleteRow(document.getElementById("row" + priceId).rowIndex);
			} else {
				alert("error!");
			}
		}  
	});
}

function formatDateTime(date) {
	var formattedDate = "";
	if (date.getDate()<10) {
		formattedDate = "0";
	}
	formattedDate = formattedDate + date.getDate() + "/";
	if (date.getMonth()<9) {
		formattedDate = formattedDate + "0";
	}
	formattedDate = formattedDate + (date.getMonth()+1) + "/";
	formattedDate = formattedDate + (date.getFullYear() - 2000);
	return formattedDate;
}


